﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace QuickResizer
{
	public static class RegistryHelper
	{
		private const string resizerKeyNamePrefix = "resizer";
		/// <summary>
		/// Builds and updates all registry key entries
		/// </summary>
		/// <param name="commandRoot">Name of command that will appear on the context menu</param>
		public static void SetRegistryKeys(ResizerItem resizerItem)
		{
			string resizerKeyName = resizerKeyNamePrefix + resizerItem.Index.ToString();
			string commandName = Resources.ContextMenuPrefix + resizerItem.Name;

			// get list of image valid extensions
			List<string> exts = GetExtensions();
			foreach (string ext in exts)
			{
				string imageKeyName = GetImageKeyName(ext);
				if (!string.IsNullOrWhiteSpace(imageKeyName))
				{
					if (resizerItem.Show)
					{
						SetCommandKey(imageKeyName, commandName, resizerKeyName, resizerItem.Index);
					}
					else
					{
						DeleteCommandKey(imageKeyName, resizerKeyName);
					}
				}
			}
		}

		public static void RemoveRegistryKeys(ResizerItem resizerItem)
		{
			string resizerKeyName = resizerKeyNamePrefix + resizerItem.Index.ToString();
			// get list of image valid extensions
			List<string> exts = GetExtensions();
			foreach (string ext in exts)
			{
				string imageKeyName = GetImageKeyName(ext);
				if (!string.IsNullOrWhiteSpace(imageKeyName))
				{
					DeleteCommandKey(imageKeyName, resizerKeyName);
				}
			}
		}

		/// <summary>
		/// Retrieves the base image key that represents the image type
		/// </summary>
		/// <param name="extension"></param>
		/// <returns></returns>
		private static string GetImageKeyName(string extension)
		{
			// find the file extension key
			RegistryKey fileExtKey = Registry.ClassesRoot.OpenSubKey(extension);
			if (fileExtKey != null)
			{
				// find the image key that the file key points to
				string imageKeyName = fileExtKey.GetValue("").ToString();
				if (!string.IsNullOrWhiteSpace(imageKeyName))
				{
					return imageKeyName;
				}
			}

			return null;
		}

		/// <summary>
		/// Sets the command for the specified image key
		/// </summary>
		/// <param name="imageKeyName"></param>
		/// <param name="commandName"></param>
		/// <param name="resizerIndex"></param>
		private static void SetCommandKey(string imageKeyName, string commandName, string resizerKeyName, int resizerIndex)
		{
			RegistryKey imageShellKey = Registry.ClassesRoot.OpenSubKey(imageKeyName + "\\shell", true);
			if (imageShellKey != null)
			{
				RegistryKey resizerKey = imageShellKey.OpenSubKey(resizerKeyName, true);
				if (resizerKey == null)  //create the key if it does not exist
					resizerKey = imageShellKey.CreateSubKey(resizerKeyName);
				resizerKey.SetValue("", commandName);
				// now set the command key with the call to the exe
				RegistryKey commandKey = resizerKey.OpenSubKey("command", true);
				if (commandKey == null)  //create the key if it does not exist
					commandKey = resizerKey.CreateSubKey("command");
				commandKey.SetValue("", Application.ExecutablePath + " \"" + resizerIndex.ToString() + "\" \"%1\"");
			}

		}

		/// <summary>
		/// Deletes registry subkey for the passed in image type and command
		/// </summary>
		/// <param name="imageKeyName"></param>
		/// <param name="resizerKeyName"></param>
		private static void DeleteCommandKey(string imageKeyName, string resizerKeyName)
		{
			RegistryKey imageShellKey = Registry.ClassesRoot.OpenSubKey(imageKeyName + "\\shell", true);
			if (imageShellKey != null)
			{
				if (imageShellKey.GetSubKeyNames().Contains(resizerKeyName))
				{
					imageShellKey.DeleteSubKeyTree(resizerKeyName);
				}
			}

		}

		private static List<string> GetExtensions()
		{
			List<string> exts = new List<string>();

			exts.Add(".jpg");
			exts.Add(".gif");
			exts.Add(".png");

			return exts;
		}
	}
}
